import os
from fire import Fire

def tostr(li):
    s = ""
    for i in li:
        s = s+str(i)
    return s

dict = "./dict.txt"
with open(dict,encoding='utf-8') as f:
    dict = f.readlines()
    dict = tostr(dict).split("\n")
    d1 = []
    d2 = []
    for i in range(0, len(dict), 2):
        d1.append(dict[i])
        d2.append(dict[i + 1])


def to_python_file(file, out):
    with open(file,encoding='utf-8') as f:
        command = f.readlines()
        list_commmand = tostr(command).split(" ")
        complie = list()
        for i in range(0, len(list_commmand)):
            part_time_use = str(list_commmand[i])
            for x in range(len(d1)):
                part_time_use = part_time_use.replace(d1[x], d2[x])
            complie.append(part_time_use)
    with open(out, mode="w",encoding='utf-8') as f:
        f.writelines(complie)


def run(file):
    to_python_file(file, "./out.py")
    os.system("python out.py")
    os.remove("out.py")


if __name__ == '__main__':
    Fire({"编译": to_python_file, "运行": run})
